package com.alpha.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.Exam;
import com.alpha.bom.Professor;
import com.alpha.dao.ExamDao;
import com.alpha.dao.ProfessorDao;

/**
 * Servlet implementation class PracticeExamServlet
 */
@WebServlet("/PracticeExamServlet")
public class PracticeExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(PracticeExamServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PracticeExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
		
		HttpSession session = request.getSession();
		
		//Do not allow a professor to access this
		if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Professor)){
			response.sendRedirect("403.jsp");
			return;
		}
		
		// TODO Auto-generated method stub
		ArrayList<Exam> exams = new ArrayList<Exam>(); 
		ArrayList<Professor> professors = new ArrayList<Professor>();
		ExamDao eDao = new ExamDao();
		ProfessorDao pDao = new ProfessorDao();
		
		try {
			exams = eDao.retrieveAllPracticeExams();
			professors = pDao.retrieveProfessors();
		} catch (NamingException e) {
			logger.error(e.getMessage());
			return;
		} catch (SQLException e) {
			logger.error(e.getMessage());
			return;
		}
		
		request.setAttribute("exams", exams);
		
		if(professors!=null){
			request.setAttribute("professors", professors);
		}
		
		RequestDispatcher dis=request.getRequestDispatcher("PracticeExams.jsp");
        dis.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
	}

}
